package org.oslims.server.dao.hbm;

import java.util.List;

import org.hibernate.Query;
import org.oslims.domain.Department;
import org.oslims.server.dao.DepartmentDao;

public class HbmDepartmentDao extends HbmGenericDao<Department, Long> implements DepartmentDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<Department> findAllWithLabResp(String sortBy) {
		Query q = this.getSession().createQuery("select distinct d from Department d left join fetch d.labStaff order by d.name");
		return q.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Department> findAllWithLabResp(int start, int length, String sortBy, Boolean desc) {

		String sortDirection = "asc";

		if (desc)
			sortDirection = "desc";

		Query q = this.getSession().createQuery("select distinct d from Department d left join fetch d.labStaff order by d." + sortBy + " " + sortDirection);
		q.setFirstResult(start);
		q.setMaxResults(length);

		return q.list();
	}

}
